<?php

namespace App\depository;

use App\Models\RecordTeamModel;

class RecordTeamDep
{
    public $model;

    public function __construct(){
        $this->model = new RecordTeamModel();
    }

    public function add($data){
        return $this->model->insert($data);
    }

    public function get_list_by_gh_id($gh_id){
        return $this->model->where('gh_id', $gh_id)->orderBy('id', 'desc')->limit(200)->get()->toArray();
    }

    public function list($param){
        $res = $this->model
            ->when(!empty($param['id']), function ($query) use ($param) {
                return $query->where("id", $param['id']);
            })
            ->when(!empty($param['gh_id']), function ($query) use ($param) {
                return $query->where("gh_id", $param['gh_id']);
            })

            ->when(!empty($param['date'][0]),function($query) use($param){
                return $query->where("date",">=",$param['date'][0]);
            })
//            ->when(!empty($param['date'][1]),function($query) use($param){
//                return $query->where("date","<=",date("Y-m-d",strtotime("+1 day",strtotime($param['date'][1]))));
//            })
            ->when(!empty($param['date'][1]),function($query) use($param){
                return $query->where("date","<=",$param['date'][1]);
            })
            ->when(!empty($param['steamid']), function ($query) use ($param) {
                return $query->where("steamid",'like', "%{$param['steamid']}%");
            })
            ->when(!empty($param['player_name']), function ($query) use ($param) {
                return $query->where("player_name",'like', "%{$param['player_name']}%");
            })
            ->when(!empty($param['team_id']), function ($query) use ($param) {
                return $query->where("team_id",$param['team_id']);
            })
            ->when(!empty($param['team_name']), function ($query) use ($param) {
                return $query->where("team_name",'like', "%{$param['team_name']}%");
            })
            ->when(!empty($param['camp']), function ($query) use ($param) {
                return $query->where("camp",'like', "%{$param['camp']}%");
            })
            ->orderBy('id', 'desc')
            ->paginate($param['per_page'], ['*'], 'page', $param['current_page']);;
        return $res;
    }

    public function del($id){
        return $this->model->whereIn('id',$id)->delete();
    }

    public function upd($id,$data){
        return $this->model->where('id',$id)->update($data);
    }

    public function getRecent($teamId,$steamid)
    {
        $res = $this->model->where('gh_id',$teamId)->where('steamid',$steamid)->orderBy('id', 'desc')->limit(50)->get();
        return $res;
    }
}
